package com.cloud.phone.broadcastReceiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import com.cloud.phone.MainActivity
import com.cloud.phone.service.CloudService

/**
 * @auth: njb
 * @date: 2024/4/17 14:22
 * @desc: 开机广播
 */
class CloudBootServiceReceiver:BroadcastReceiver() {
    private val TAG = "CloudPhone"
    override fun onReceive(context: Context, intent: Intent?) {
        if (intent?.action.equals(Intent.ACTION_BOOT_COMPLETED)) {
            val serviceIntent = Intent(context, CloudService::class.java)
            when {
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
                    context.startForegroundService(serviceIntent)
                }
                else -> {
                    context.startService(serviceIntent)
                }
            }
            startActivityFromService(context)
        }
    }

    private fun startActivityFromService(context: Context) {
        val intent = Intent(context, MainActivity::class.java)
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        context.startActivity(intent)
    }
}